import queue
import hashlib


class ManageBase(object):
    url_queue = queue.Queue(50000)  # request对象储存器
    fingerprints = set()  # url去重
    __instance = None  # 是否是第一次创建

    def __new__(cls, *args, **kwargs):
        '''
        保证管理器是唯一对象,单实例模式
        :return: obj
        '''
        if cls.__instance is None:
            cls.__instance = object.__new__(cls)

        return cls.__instance

    def set_md5(self, request):
        '''
        创建一个HTTP request的md5编码
        '''
        obj = hashlib.md5()
        obj.update(bytes(request, encoding="utf-8"))
        return obj.hexdigest()

    def gets_is_download(self, request_obj):
        '''
        拿出并且返回对象,实现去重
        :return: HttpRequest
        '''
        md5 = self.set_md5(str(request_obj))

        if not (md5 in self.fingerprints):
            self.fingerprints.add(md5)
            self.url_queue.put(request_obj)

        return None



